﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Microsoft.Windows.Controls.Logic {
    
    public class CommandMapper: ICommandMapper {
        public EditCommand KeyPress(KeyEventArgs args) {
            EditCommand modifier = EditCommand.None;
            var shift = (Keyboard.Modifiers & ModifierKeys.Shift) != 0;
            if (shift)
                modifier = EditCommand.ExtendSelect;
            bool ctrl = (Keyboard.Modifiers & ModifierKeys.Control) != 0;
            switch (args.Key) {
                case Key.Back:
                    if (ctrl)
                        return EditCommand.DeleteWordLeft;
                    else
                        return EditCommand.DeleteBack; 
                case Key.Delete: return EditCommand.DeleteForward; 
                case Key.Down:
                    if (ctrl)
                        return EditCommand.ScrollDown | modifier;
                    else
                        return EditCommand.MoveLineDown | modifier; 
                case Key.End:
                    if (ctrl)
                        return EditCommand.MoveToEndOfDocument | modifier;
                    else
                        return EditCommand.MoveToEndOfLine | modifier; 
                case Key.Enter: return EditCommand.InsertNewLine;
                case Key.Home:
                    if (ctrl)
                        return EditCommand.MoveToStartOfDocument | modifier;
                    else
                        return EditCommand.MoveToStartOfLine | modifier;
                case Key.Insert: return EditCommand.ToggleInsert; 
                case Key.Left:
                    if (ctrl)
                        return EditCommand.PreviousWord | modifier;
                    else
                        return EditCommand.PreviousCharacter | modifier;
                case Key.PageDown:
                    if (ctrl)
                        return EditCommand.MoveToBottomOfPage | modifier;
                    else
                        return EditCommand.PageDown | modifier;
                case Key.PageUp:
                    if (ctrl)
                        return EditCommand.MoveToTopOfPage | modifier;
                    else
                        return EditCommand.PageUp | modifier;
                case Key.Right:
                    if (ctrl)
                        return EditCommand.NextWord | modifier;
                    else
                        return EditCommand.NextCharacter | modifier;
                case Key.Tab: return EditCommand.InsertTab;
                case Key.Up:
                    if (ctrl)
                        return EditCommand.ScrollUp | modifier;
                    else
                        return EditCommand.MoveLineUp | modifier;
                case Key.C:
                    if (ctrl)
                        return EditCommand.Copy;
                    goto default;
                case Key.V:
                    if (ctrl)
                        return EditCommand.Paste;
                    goto default;
                case Key.X:
                    if (ctrl)
                        return EditCommand.Cut;
                    goto default;
                case Key.Y:
                    if (ctrl)
                        return EditCommand.Redo;
                    goto default;
                case Key.Z:
                    if (ctrl) 
                        return EditCommand.Undo; 
                    goto default;
                case Key.P:
                    if (ctrl && shift)
                        return EditCommand.MacroPlay;
                    goto default;
                case Key.R:
                    if (ctrl && shift)
                        return EditCommand.MacroRecordToggle;
                    goto default;
                                    
                default: return EditCommand.None;
            }
        }
    }
}
